jQuery(document).ready(function(){

        jQuery('#change_button').click(function() {
                var form = jQuery(this).parent().parent();
                if(form.valid())
                    {
                    jQuery.ajax( {
                            type: "POST",
                            url: form.attr( 'action' ),
                            data: form.serialize(),
                            success: function( response ) {
                                _abpv_hideLoading();
                                form.each (function(){
                                        this.reset();
                                });
                                alert(response);                
                            }
                    } );  
                }  

        });
        // auto center the loading
        jQuery.fn.centerScreen = function(loaded) {  
            var obj = this; 
            obj.css('top', $(window).height()/2- this.height()/2); 
            obj.css('left', $(window).width()/2- this.width()/2); 
            return;
            if(!loaded) { 
                obj.css('top', $(window).height()/2- this.height()/2); 
                obj.css('left', $(window).width()/2- this.width()/2); 
                $(window).resize(function() 
                    { obj.centerScreen(!loaded); }); 
            } else { 
                obj.stop(); 
                obj.animate({ top: $(window).height()/2 - this.height()/2, left: $(window).width()/2-this.width()/2}, 200); 
            } 
        };
        // center the loading         
        //_abpv_centerLoading();
        jQuery("#abpv_loadingDiv").centerScreen(true);
        $(window).bind('resize', function() {
                jQuery("#abpv_loadingDiv").centerScreen(false);
        });
        $(window).bind('scroll', function() {
                jQuery("#abpv_loadingDiv").centerScreen(false);
        });

        // documents tab
        _documents_init_dialog();
        _invoice_init_dialog();
        _case_init_dialog();   
        jQuery("#mettingSubmit").click(function(){
                if (jQuery('#meeting_request').valid()){
                    var my_date=0;
                    // check date
                    try
                    {
                        hour=jQuery("#hour").combobox('getValues');
                        minute=jQuery("#minute").combobox('getValues')
                        if(isNumber(minute)&&isNumber(hour)&&minute<=60&&hour<=24)
                            {
                            date_string=(jQuery("#date_meeting").datebox("getValue")+" "+hour+":"+minute);
                            my_date = new Date(date_string);
                        }
                        else
                            {
                            alert(lang.meeting_formdata_time);
                            return;
                        }
                    }
                    catch (ex){
                        alert(lang.meeting_formdata_datemissing);
                        return;
                    }
                    if (my_date == "Invalid Date")
                        {
                        alert(lang.meeting_formdata_datemissing);
                        return;
                    }
                    else
                        {
                        if (my_date < new Date() )
                            {
                            alert(lang.meeting_formdata_date_early);
                            return;
                        }
                        else
                            {
                            jQuery("#date_meeting").datebox("setValue",date_string)
                        }
                    }
                    // check for mentor request
                    if (jQuery("#mentor_request").combobox("getValue")=="1")
                        {
                        // check if location and language is empty or not
                        if (jQuery("#mentor_location").val() == "" || jQuery("#mentor_language").val())
                            {
                            alert(lang.meeting_formdata_mentor_infomissing);
                            return;
                        }
                    }
                    _abpv_showLoading();
                    jQuery.post('/index.php', jQuery("#meeting_request").serialize(), function(data) {
                            _abpv_hideLoading();
                            if(data=="success"){
                                alert(lang.thanks_for_send);
                                jQuery("#meeting_request").get(0).reset();
                            }
                            else
                                alert(lang.error_meeting);
                    });
                }
                else
                    {
                    alert(lang.meeting_formdata_missing);
                }
        });


        jQuery("input[name='hour']").parent().find("input[type='text']").attr("readonly","readonly");

        jQuery("input[name='minute']").parent().find("input[type='text']").attr("readonly","readonly");
})


function isNumber(num) {
    if(num>=0)
        {
        return (num % 1 == 0);
    }
    else
        return false;
}

function _save_profiles()
{
    _abpv_showLoading();
    var form = jQuery('#profile_form');

    jQuery.ajax( {
            type: "POST",
            url: form.attr( 'action' ),
            data: form.serialize(),
            success: function( response ) {
                _abpv_hideLoading();
                if(response=="success"){
                    alert(lang.update_success);
                }
                else
                    {
                    alert(lang.update_error);
                }
            }
    } );


}



function _abpv_showLoading(){
    jQuery('#abpv_loadingDiv').fadeIn();
}

function _abpv_hideLoading(){
    jQuery('#abpv_loadingDiv').fadeOut();
}

// Emulate form helper get src
function __FormHelper_GetSrc(){
    return "";
}


function _documents_clearfilter()
{
    try{
        jQuery("#_document_form_advancesearch_name").val("");
        jQuery("#_document_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_document_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_document_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}

function _invoices_clearfilter()
{
    try{
        jQuery("#_invoices_form_advancesearch_name").val("");
        jQuery("#_invoices_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_invoices_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_invoices_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}
function _cases_clearfilter()
{
    try{
        jQuery("#_case_form_advancesearch_name").val("");
        jQuery("#_case_form_advancesearch_datefrom").datebox("setValue","");
        jQuery("#_case_form_advancesearch_dateto").datebox("setValue","");
        jQuery("#_case_form_advancesearch_filetype").combobox("setValue","none");
    }
    catch (err)
    {

    }
}
function _documents_reload()
{
    jQuery('#_tab_profile_document').datagrid("reload");
}
function _gird_reload(mod)
{
    if(jQuery('#_tab_profile'+mod).length >0)
        jQuery('#_tab_profile'+mod).datagrid("reload");
}
/* End documents */

function send_meeting()
{
    if (jQuery('#meeting_form').valid()){
        jQuery("#mess_download").html(lang.sending_request);
        $("#click_show_download").eq(0).trigger("click");
        jQuery.ajax({
                type: "POST",
                url: "/index.php",
                data: jQuery("#meeting_form").serialize(),
                success: function(data)
                {
                    if(data=='success')
                        mes=lang.thanks_for_send;
                    else
                        mes=lang.error_meeting;
                    jQuery("#mess_download").html(mes);
                    window.setTimeout(function(){jQuery.fancybox.close();; },3000);

                }
        });
        jQuery('#meeting_form')[0].reset();
    }
}


function go_page(e,mod)
{
    if(mod=='document'){
        tab=2;
    }
    else
        tab=1;
    page_num=jQuery(e).attr('rev');
    if(page_tab[tab][page_num])
        {
        jQuery('#tabs-'+mod).html(page_tab[tab][page_num]);
        return;
    }
    jQuery("#mess_download").html(lang.loading);
    $("#click_show_download").eq(0).trigger("click");

    search_key='';
    search_order='post_date';

    jQuery.ajax({
            type: "POST",
            url: "/index.php",
            data: { module: "profilemodule", action: mod+"_main",order_by: search_order,search:search_key,ajax_action:1,page:page_num}
    }).done(function( msg ) {

            page_tab[tab][page_num]=msg;
            jQuery('#tabs-'+mod).html(msg);
            jQuery.fancybox.close();
    });
}
//--------TAB-----------
jQuery(document).ready(function(){
        jQuery(".linktabs_item").each(function (index, item){
                var wrapper_heigh = jQuery(".linktabs_wrapper").height();
                //if (jQuery(item).height() > wrapper_heigh )
                //jQuery(".linktabs_wrapper").css("height",jQuery(item).height()+"px").parents(".container_modulewrapper").css("height",jQuery(item).height()+120+"px").parents(".main-wrapper").css("height",jQuery(item).height()+185+"px");
        });

        jQuery(".banner-navigation .banner-navigation-image .banner_a").click(function(){

                var selected_a = jQuery(".linktabs_selected").parent();

                var r=jQuery(this).attr("rel") ;
                var loaded=jQuery(this).attr("rev");
                if (selected_a.attr("rel") == r)
                    return;
                if(loaded!=1)
                    {
                    _gird_reload(r);
                    jQuery(this).attr("rev",1);
                }
                jQuery(".linktabs_wrapper .linktabs_item").hide();
                jQuery(".linktabs_wrapper .linktabs_item[rel='"+r+"']").fadeIn();
                jQuery(".ui-state-default").removeClass("ui-tabs-selected ui-state-active");
                jQuery(".linktabs_selected").removeClass("linktabs_selected");
                jQuery(this).find(":first-child").addClass("linktabs_selected");
                jQuery(this).parent().addClass("ui-tabs-selected ui-state-active");

        });
});
//Document
function _documents_init_dialog(){
    // init documents
    jQuery('#_tab_profile_document').datagrid({  
            title:lang.list_document,
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            width:890,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[          
                    {field:'name',title:lang.doc_name,width:266, sortable:true},
                    {field:'create_by',title:lang.create_by,width:170, sortable:true},
                    {field:'post_date',title:lang.create_on,width:170, sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'file_list',title:lang.media_file,width:250, formatter: function(value,row,index){
                            var html="";
                            if (row.flv_file!="")
                                html+="<a href='"+row.flv_file+"' class='icon-video grid_a' target=_blank>&nbsp;</a>";
                            if (row.mp3_file!= "")
                                html+="<a href='"+row.mp3_file+"' class='icon-mp3 grid_a' target=_blank>&nbsp;</a>"
                            if (row.pdf_file!= "")
                                html+="<a href='"+row.pdf_file+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>"
                            if (row.doc_file!= "")
                                html+="<a href='"+row.doc_file+"' class='icon-doc grid_a' target=_blank>&nbsp;</a>"
                            if (row.xls_file!= "")
                                html+="<a href='"+row.xls_file+"' class='icon-xls grid_a' target=_blank>&nbsp;</a>"
                            if (row.ppt_file!= "")
                                html+="<a href='"+row.ppt_file+"' class='icon-ppt grid_a' target=_blank>&nbsp;</a>"
                            if (row.img_file!= "")
                                html+="<a href='"+row.img_file+"' class='icon-img grid_a' target=_blank>&nbsp;</a>"
                            return html;
                    }},
                ]],
            onBeforeLoad: function(param){
                // get selected user in main grid
                var url="/index.php?module=profilemodule&action=manage_documents_ajax&ajax_action=1&user_id="+_user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_document_form_advancesearch_name").val();

                if(search_name!=''){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_from=jQuery("#_document_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_document_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to;

                var file_type =jQuery("#_document_form_advancesearch_filetype").combobox("getValue");
                url+="&filetype="+file_type;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }  
    });  
}


//Invoice
function _invoice_init_dialog()
{
    jQuery('#_tab_profile_invoices').datagrid({  
            title:lang.list_invoices,
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            width:890,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[  
                    {field:'name',title:lang.invoice_name,width:266, sortable:true},
                    {field:'create_by',title:lang.create_by,width:170, sortable:true},
                    {field:'post_date',title:lang.create_on,width:170, sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'file_list',title:lang.invoice_file,width:250, formatter: function(value,row,index){
                            var html="";
                            if (row.pdf_file!= "")
                                html+="<a href='"+row.pdf_file+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>";
                            if (row.img_file!= "")
                                html+="<a href='"+row.img_file+"' class='icon-img grid_a' target=_blank>&nbsp;</a>"    
                                
                            return html;
                    }},
                ]],
            onBeforeLoad: function(param){
                // get selected user in main grid


                var url="/index.php?module=profilemodule&action=manage_invoices_ajax&ajax_action=1&user_id="+_user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_invoices_form_advancesearch_name").val();

                if(search_name!='' &&search_name){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_from=jQuery("#_invoices_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_invoices_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to;


                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }    
    });

}
//Case
function _case_init_dialog()
{
    jQuery('#_tab_profile_cases').datagrid({  
            title:lang.list_of_cleint_cases,
            autoRowHeight: true, 
            idField: "id",
            nowrap: false,
            height: 565,
            width:890,
            pageNumber: 1,
            pageSize: 20,
            pagination: 1,
            rownumbers: true,
            singleSelect: true, 
            columns:[[  
                    {field:'name',title:lang.casename,width:213, sortable:true},  
                    {field:'poster',title:lang.create_by,width:150, sortable:true},  
                    {field:'post_date',title:lang.create_on,width:140,align:'right',sortable:true, formatter: function(value,row,index){
                            return row.post_date_text;
                    }},
                    {field:'last_update',title:lang.last_update,width:150,align:'right',sortable:true, formatter: function(value,row,index){
                            return row.last_update_text;
                    }} , 
                    {field:'last_update_by',title:lang.last_update_by,width:100,align:'right', sortable:true}  ,
                    {field:'pdf_file',title:lang.pdf_video,width:100,align:'right', formatter: function(value,row,index){
                            var html="";
                            if (row.file_video!="")
                                html+="<a href='"+row.file_video+"' class='icon-video grid_a' target=_blank>&nbsp;</a>";
                            if (row.file_pdf!= "")
                                html+="<a href='"+row.file_pdf+"' class='icon-pdf grid_a' target=_blank>&nbsp;</a>"
                            return html;
                    }},
                ]]  ,
            onBeforeLoad: function(param){
                // get selected user in main grid


                var url="/index.php?module=profilemodule&action=manage_cases_ajax&ajax_action=1&user_id="+_user_id+"&src="+__FormHelper_GetSrc();
                // build URL to reload
                var options=jQuery(this).datagrid("options");

                var search_name=jQuery("#_case_form_advancesearch_name").val();

                if(search_name!=''){
                    url += "&search="+search_name;
                }
                if (options.sortName != null){
                    url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
                }
                // date search type
                var date_type =jQuery("#_case_form_advancesearch_datetype").combobox("getValue");
                var date_from=jQuery("#_case_form_advancesearch_datefrom").datebox("getValue");
                var date_to=jQuery("#_case_form_advancesearch_dateto").datebox("getValue");
                url+="&datefrom="+date_from+"&dateto="+date_to+"&datetype="+date_type;

                var file_type =jQuery("#_case_form_advancesearch_filetype").combobox("getValue");
                url+="&filetype="+file_type;

                url+="&_"+Math.random();

                jQuery(this).datagrid('options').url = url;
            }
    }); 
}